home *** CD-ROM | disk | FTP | other *** search
/ Collection of Internet / Collection of Internet.iso / msdos / lynx / source / doslynx / src / capalloc.cpp next >
Encoding:
C/C++ Source or Header  |  1994-10-25  |  2.7 KB  |  97 lines

  1. //    Copyright (c) 1994, University of Kansas, All Rights Reserved
  2. //
  3. //    Include File:    capalloc.h
  4. //    Purpose:    Kludge the C memory functions to call new and delete.
  5. //    Remarks/Portability/Dependencies/Restrictions:
  6. //        Functions are callable by both C and C++ source.
  7. //    Revision History:
  8. //        02-25-94    created
  9. #include"capalloc.h"
  10. #include<string.h>
  11.  
  12. extern "C"    {
  13.  
  14. void *capmalloc(size_t size)    {
  15. //    Purpose:    Mimic malloc with new.
  16. //    Arguments:    size    The amount of memory to dynamically allocate.
  17. //    Return Value:    void *    A pointer to the allocated memory or
  18. //                NULL on failure.
  19. //    Remarks/Portability/Dependencies/Restrictions:
  20. //    Revision History:
  21. //        02-25-94    created
  22.  
  23.     return((void *)new char[size]);
  24. }
  25.  
  26. void *capcalloc(size_t nitems, size_t size)    {
  27. //    Purpose:    Mimic calloc with new.
  28. //    Arguments:      nitems    The number of items of size to allocate.
  29. //            size    The amount of memory to dynamically allocate
  30. //                for each item.
  31. //    Return Value:    void *    A pointer to the allocated memory or
  32. //                NULL on failure.
  33. //    Remarks/Portability/Dependencies/Restrictions:
  34. //    Revision History:
  35. //        02-25-94    created
  36.  
  37.     //    Allocate and initialize the block.
  38.     char *cp_block = new char[nitems * size];
  39.     if(cp_block == NULL)    {
  40.         return(NULL);
  41.     }
  42.  
  43.     for(size_t count = 0; count < nitems * size; count++)    {
  44.         *(cp_block + count) = '\0';
  45.     }
  46.  
  47.     return((void *)cp_block);
  48. }
  49.  
  50. void *caprealloc(void *block, size_t size)    {
  51. //    Purpose:    Mimic realloc with new and delete.
  52. //    Arguments:    block    A pointer to the memory to resize.
  53. //            size    The new size of the block.
  54. //    Return Value:    void *    A pointer to the new block of size, or
  55. //                NULL if can't resize the block.
  56. //    Remarks/Portability/Dependencies/Restrictions:
  57. //        If block is NULL to begin with, acts just like malloc.
  58. //        If size is 0, then will do nothing and return NULL.
  59. //    Revision History:
  60. //        02-25-94    created
  61.  
  62.     if(block == NULL)    {
  63.         return(capmalloc(size));
  64.     }
  65.     else if(size == 0)    {
  66.         return(NULL);
  67.     }
  68.  
  69.     //    First allocate a new block, if can't return.
  70.     void *newblock = capmalloc(size);
  71.     if(newblock == NULL)
  72.         return(NULL);
  73.  
  74.     //    Since really don't know size of old block, copy over size
  75.     //    bytes from oldblock to new block.
  76.     memcpy(newblock, block, size);
  77.  
  78.     //    Release the old block, return the new.
  79.     capfree(block);
  80.     return(newblock);
  81. }
  82.  
  83. void capfree(void *block)    {
  84. //    Purpose:    Mimic free with delete.
  85. //    Arguments:    block    A pointer to the memory to be freed.
  86. //    Return Value:    void
  87. //    Remarks/Portability/Dependencies/Restrictions:
  88. //        Memory to be freed is expected to be created using one of
  89. //        the above functions.  If it is not, results, as I know of,
  90. //        will be undefined.
  91. //    Revision History:
  92. //        02-25-94    created
  93.  
  94.     delete[]((char *)block);
  95. }
  96.  
  97. }; // extern "C"